package cn.itcast.web.controller.company;

import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.company.Company;
import cn.itcast.domain.vo.PageBean;
import cn.itcast.service.company.CompanyService;
import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/company", name = "企业管理")
public class CompanyController extends BaseController {

    @Autowired
    private CompanyService companyService;

    @RequestMapping(value = "/list", name = "查询企业")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {
      /*  List<Company> list = companyService.findAll();
        request.setAttribute("list", list);*/

        /*PageBean pageBean = companyService.findByPage(pageNum, pageSize);
        request.setAttribute("page", pageBean);*/

       PageInfo page =  companyService.findByPageHelper(pageNum,pageSize);
       request.setAttribute("page", page);

        return "company/company-list";
    }

    @RequestMapping(value = "/toAdd", name = "新增页面")
    public String toAdd() {
        return "company/company-add";
    }

    @RequestMapping(value = "/edit", name = "新增/修改")
    public String edit(Company company) {

        if (StrUtil.hasEmpty(company.getId())) { // id为空，新增
            companyService.save(company);
        } else { // id不为空，修改
            companyService.update(company);
        }

        return "redirect:/company/list.do";
    }

    @RequestMapping(value = "/toUpdate", name = "编辑页面")
    public String toUpdate(String id) {
        Company company = companyService.findById(id);
        request.setAttribute("company", company);
        return "company/company-update";
    }

    @RequestMapping(value = "/delete", name = "删除")
    public String delete(String id) {

        companyService.delete(id);

        return "redirect:/company/list.do";
    }
}
